Helpful Docker commands
Create and set a default working directory within a Docker image file system.
WORKDIR <path/to/workdir>
Copy files and directories from source to a Docker image file system.
COPY <src> <dest>
Execute command(s) and creates a new Docker image file. RUN
is often used to install dependencies.
RUN <cmd>
Set default command that will be executed on running Docker container.
CMD <cmd>
Build a Docker image from a Dockerfile.
docker build .
Build a Docker image from a Dockerfile tagging the Docker image.
docker build -t <id/name:tag> .
Naming convention of a Docker image tag.
yourDockerId/projectName:version
bdunlop/dockerized-react-app:latest
When creating and starting a Docker container from a tagged Docker image, :latest
is automatically appended when not specified.
docker run bdunlop/dockerized-react-app
Specify path to an alternate Dockerfile to use to build a Docker image. (Default is Dockerfile)
docker build -f Dockerfile.dev
Show the history of an image including image layers.
Create and starts a Docker container from a Docker image.
docker run <image>
Create and start a Docker container a Docker image in the background, printing the Docker container ID.
docker run <image> -d
Create and starts a Docker container from a Docker image, publishing a container's port(s) to the host.
docker run -p <host>:<container> <image>
By default no traffic coming into the localhost network is redirect to into the Docker container network. To redirect a request into a Docker container, an explicit PORT mapping must be set up.
The -it
instructs Docker to allocate a pseudo-TTY connected to the container’s STDIN
; creating an interactive bash
shell in the container.
docker run -it <image>
Sets a mapping from a folder or file in a Docker containter to a folder or file on the local machine.
More information required
docker run -v /usr/app/node_modules -v $(pwd):/usr/app <image>
Run a command in a running Docker container.
docker exec <container> <command>
The -it
instructs Docker to allocate a pseudo-TTY connected to the container’s STDIN
; creating an interactive bash
shell in a runnning container.
docker exec -it <container> <cmd>
Start a shell session with the default shell installed in the Docker container.
docker exec -it <container> /bin/sh
List all running Docker containers.
docker ps
List all Docker containers.
docker ps -a
Stop a running Docker container.
docker stop <container>
Kill a running Docker container.
docker kill <container>
Kill all running Docker containers.
docker kill $(docker ps -q)
Remove one or more Docker containers.
docker rm <container>
Remove all Docker containers.
docker rm $(docker ps -a -q)
Remove one or more Docker images.
docker image rm <image>
Remove all Docker images.
docker rmi $(docker images -q)
Fetch all the logs that have been emitted from a Docker container.
docker logs <container>
Builds, (re)create and starts instances of all Docker containers in a Compose configuration file.
docker-compose up
Create and start a Docker container a Docker image in the background, printing the Docker container ID.
docker-compose up -d
Build Docker images before starting Docker container.
docker-compose up --build
Stops and removes all Docker containers created by up
.
docker-compose down
List all running Docker containers that belong to the Compose configuration file.
Must be run from directory with Compose configuration file.
docker ps
List all running Docker containers that belong to the Compose configuration file.
docker ps -a
Exclude files and directories from a Docker image file system by adding a .dockerignore
file to the root directory.