docker (and docker-compose) frequently used CLI commands

docker

docker run

Creates a new container from an image and run it

docker start

Launch a container previously stopped

docker exec

Executes a command in a running container

This is usually helpful to get a shell on a running container for checking

docker exec -it <container> /bin/sh

docker-compose

docker-compose up

to start (or restart) all services defined in the docker compose file

docker-compose up

Runs in the foreground. When exiting (e.g., CTRL-C) containers are stopped

docker-compose up -d

Starts containers in the background and leaves them running

If the docker compose config file specifies to build iamges (), docker-compose up will build images only if they do not already exist (for example when running up for the first time). To rebuild images, need to do:

docker-compose build
docker-compose up --build

docker-compose start

Restart containers previously created

It does not create new containers

docker-compose run

Used for one-off tasks (e.g., tests or administrative tasks)

docker-compose stop

Stop containers (won’t remove them)

docker-compose down

Stop containers and removes stopped containers, networks, volumes and images created by up

docker-compose exec

This is equivalent to docker exec.

For example, if there is a django application defined as “web” in the compose file, to run migrations:

docker-compose exec web python manage.py migrate

To get an interactive shell:

docker-compose exec web sh

References

  1. docker-compose FAQ
  2. docker compose CLI reference
  3. docker CLI reference
updatedupdated2020-05-052020-05-05