Docker Snippets

Docker Compose Projects

We recommend always using a project name with the docker-compose -p argument.

# start two separate instances of the same docker-compose project
docker-compose -p dev_instance_1 up
docker-compose -p dev_instance_2 up

Docker compose nicely defines and contains your services and is pretty straight forward. It is not designed for large scale deployment though and a big gotcha is that if you run multiple instances on a single computer/server, you may destroy parts of your existing instance, so take care and ALWAYS use project names if you are using multiple instances on the one server. If you are using docker-compose for your development (as we) you will likely run into this.

Volumes across Instances

The danger of multiple instances extends further if you are defining volumes. For this reason if you are using volumes in your multi instance development, consider using extensible yaml files as detailed in Docker docs.

Restart Single Worker using Docker Compose

docker-compose -p dev_instance_1 restart webserver

Access docker command line

List the docker services, and then connect using docker exec.

docker ps
CONTAINER ID   IMAGE                         COMMAND                  CREATED       STATUS                  PORTS                                         NAMES
dbc1effc3903   dev_project/nginx:latest           "nginx -g 'daemon of…"   3 weeks ago   Up Less than a second   443/tcp,>80/tcp, :::81->80/tcp    dev_instance_1_nginx_1

# select identifier from list
docker exec -it dbc1effc3903 /bin/bash
bash-5.1# echo here you are on the docker cli