Docker compose

Docker compose is a tool in the Docker platform that is used to define and run multi-container applications. It lets us define how a container will behave when it is run in production, and also lets us define other services that it depends on and how services work with each other. Each application is a service as it defines the behavior of the container, for example, what port it runs on, what environment variables it uses, and so on. A YAML file is used for this. A single docker.compose.yml file can define all the services that are required for a multi-container application and can then be started with a single command. We will see more about Docker and docker -compose in Chapter 11Deploying with Docker Compose.

Visit https://docs.docker.com/get-started/ to learn more about Docker.

The following table is a list of useful commands for Docker and Docker compose:

docker build -t myapp:1.0. Build an image from the Dockerfile in the current directory and tag the image
docker images List all images that are locally stored with the Docker engine
docker pull alpine:3.4
Pull an image from a registry
docker push myrepo/myalpine:3.4 Push an image to a registry
docker login Log in to a registry (the Docker Hub, by default)
docker run --rm -it -p 5000:80 -v /dev/code alpine:3.4 /bin/sh Run a docker container

--rm: Remove container automatically after it exits
-it: Connect the container to the Terminal
-p: Expose port 5000 externally and map to port 80
-v: Create a host mapped volume inside the container
alpine:3.4: The image from which the container is instantiated
/bin/sh: The command to run inside the container

docker stop myApp Stop a running container
docker ps List the running containers
docker rm -f $(docker ps -aq) Delete all running and stopped containers
docker exec -it web bash Create a new bash process inside the container and connect it to the Terminal
docker logs --tail 100 web Print the last 100 lines of a container's logs
docker-compose up Start the services defined in the docker-compose.yml file in the current folder
docker-compose down

Stop the services defined in the docker-compose.yml file in the current folder