Useful Docker Commands

For Beginners

Sean
3 min readNov 1, 2021

Images

Building

Build a docker image from a Dockerfile in the current dir.

docker build . -t <my-image-name>:<tag>

[Note: tag is optional, docker will use latest by default, it’s a version number or name.]

Containers

Running

Run a container from an image interactively (shelled in), remove it when exited and map port 3000 to my machine’s port 3000.

docker run --rm -itp 3000:3000/tcp <my-image-name>:<tag>

Env Variables

Run a container passing in env variables.

docker run -e VAR_NAME=var_value <my-image-name>:<tag>

[Note: for multiple vars just add another -e flag for each one.]

Listing Containers

List running docker containers, optionally add the -a flag for hidden containers.

docker ps -a

Removing Containers

Stop and remove a container.

docker rm -f <container-id>

Executing Commands

Shell into a running container.

docker exec -it <container-id> /bin/bash

[Note: the end might be bin/sh for macos and some linux distros.]

Logging

View the log output from a running container.

docker logs -f <container-id>

[Note: you can CTRL-c out of this without killing the container.]

Volumes

Creating

Creating a volume.

docker volume create <my-volume-name>

Volume Mounting

Running a container and setting a volume with a directory path as it’s mount point.

docker run -v <my-volume-name>:<container-dir-path> <my-image-name>:<tag>

[Note: if we didn’t create the volume beforehand, docker will make one automatically.]

Local Mounting

Doing the same but using a bind mount (a local directory).

docker run -v <local-dir-path>:<container-dir-path> <my-image-name>:<tag>

Inspecting a Volume

Print the options and location of any volume.

docker volume inspect <my-volume-name>

Docker Hub

Login

Log in to your docker repo.

docker login -u <username>

Adding a Remote

Create a target image (username/my-image-name) that refers to a source image (my-image-name).

docker tag <my-image-name>:<tag> <username>/<my-image-name>:<tag>

[Note: this is a bit like git remote add origin.]

Pushing

Push to the repo.

docker push <username>/<my-image-name>:<tag>

Networks

Creating

Create a network.

docker network create <my-network-name>

Attaching to a Container

Start a container and attach it to a given network.

docker run --network <my-network-name> <my-image-name>:<tag>

[Note: you can attach more containers to this same network but networking only works between containers on the same host.]

Network Alias

Set an alias to the network to identify it from inside the container.

docker run --network <my-network-name> --network-alias <my-alias> <my-image-name:<tag>

Docker Compose

Comparison

Here’s a long docker CLI command:

docker run -dp 3000:3000 \
-w /app -v "$(pwd):/app" \
--network todo-app \
-e MYSQL_HOST=mysql \
-e MYSQL_USER=root \
-e MYSQL_ROOT_PASSWORD=secret \
-e MYSQL_DB=todos \
node:12-alpine \
sh -c "yarn install && yarn run dev"

This is it’s equivalent docker-compose yaml:

version: "3.7"services:
app:
image: node:12-alpine
command: sh -c "yarn install && yarn run dev"
ports:
- 3000:3000
working_dir: /app
volumes:
- ./:/app
environment:
MYSQL_HOST: mysql
MYSQL_USER: root
MYSQL_ROOT_PASSWORD: secret
MYSQL_DB: todos

[Note: the app: key below services: automatically becomes a network alias.]

Volumes

Adding volumes doesn’t work automatically like in the cli, you have to define a volumes entry at the same level as services:

version: "3.7"services:
todoapp:
image: node:12-alpine
command: sh -c "yarn install && yarn run dev"
ports:
- 3000:3000
working_dir: /todoapp
volumes:
my-todoapp-volume:/todoapp
volumes:
my-todoapp-volume:

--

--

No responses yet