The Docker Book

Common tasks with docker and docker-compose

How to create a container

$ docker create <image-name>

The command above will create a container based on the docker image whose name is provided and returns an id for the container is has created. It will not start the container.

How to start a container

$ docker start -a <container-id>
$ docker start --attach <container-id>

The command above will start up a container that has already been created by using the provided container id. It will run the default start-up command associated with the container to set it up (Two alternative versions of the command have been provided)

How to run a container

$ docker run <image-name>
$ docker run <image-name> <alternate-startup-command>
$ docker run --detach <image-name>

The first command above will create and start a container based on the docker image whose name is provided. It will run the default start-up command associated with the container to set it up (image will be pulled down from dockerhub automatically if it is not available locally)

The second command above will create and start a container based on the docker image whose name is provided. It will run the alternate startup command provided, however, instead of the default startup command associated with the container to set it up.

The first command above will create and start a container based on the docker image whose name is provided. It will run the default start-up command associated with the container to set it up; but it will run the container in the background and return control back to the shell in which it was run.

How to list all containers

The first command lists all containers currently running

The second command lists all containers that have ever been run

The third command is an alternate form of the second command

How to delete all containers

The command above will delete all containers

How to get logs from a running container

The command above will print logs for the container whose id is provided

How to terminate a container

The first command requests the container whose id was passed to shut down. It waits 10 seconds and if the container has not shut down it kills it.

The second command kills the container whose id was provided without waiting 10 seconds right away.

How to run multiple container commands

The command above attaches an interactive teletype to the running container whose id was provided and runs the provided command inside of the container.

A user can run the above multiple times, using a different command to run in the container each time.

How to get shell access to a container

The first command provides interactive shell access to a container that is running.

The second command creates and starts a container based on the provided image id. It runs the shell command instead of running the startup command associated with the container.

How to build a custom image from a Dockerfile

Dockerfile defines a docker file. The instructions inside it define how to build a custom redis image. The file must be called Dockerfile and can only contain Docker commands and comments.

Build image defines what is run from the folder in which the Dockerfile resides. It executes the instructions inside the Dockerfile to create an image with the name debugme/redis:latest and the dot is used to specify which directory the resources to include in the building of the image can be found in. The build command allows you to specify an alternate name for the Docker file with the --file flag.

Run image shows two equivalent ways to run this custom image. If latest is omitted, then it is assumed to be present by default.

How to build a tagged image from a container

How to run a custom server in a docker container

Dockerfile: instructions on how to create a custom image

Build image: the command at the terminal to build the image

Run image: terminal command to run image as a container

package.json: dependencies and scripts to build and run server

index.js: a JS code implementation for a simple server

How to delete all Docker images that have been downloaded

Docker-compose commands

How to run an application composed of multiple docker containers

How to specify a restart policy for your docker container

How to sync local frontend changes with Docker

How to sync local frontend changes with Docker Compose

How to sync local backend changes with Docker Compose

How to create a container using a multi-step approach

How to deploy code changes to GitHub and test pull requests on TravisCI

How to get container id via image name

Last updated