Historical archive
Essential Docker Commands, Nginx Containers, and Dockerfiles
A practical Docker command reference covering containers, images, bind mounts, Nginx, Dockerfiles, and the historical Ubuntu installation workflow.
Docker makes it easier to reproduce environments, deploy one service across several environments, or isolate several service environments on one server.
Install Docker using the current instructions from docker.com.
Basic commands
# Show running containers.
docker ps
# Show all containers.
docker ps -a
# Create an interactive Ubuntu container with a terminal.
docker run -it ubuntu
# Start an existing container.
docker start <ID>
# Attach to a running container.
docker attach <ID>
# Detach without stopping: Ctrl+P, then Ctrl+Q.
# Stop a container.
docker stop <ID>
# Remove a container.
docker rm <ID>
# List local images.
docker image ls
# Remove an image.
docker rmi <REPOSITORY>
# Show filesystem changes in a container.
docker diff <ID>
# Create an image from a container.
docker commit <ID> <USER_NAME/IMAGE_NAME:IMAGE_VERSION>
# Build an image from a Dockerfile.
docker build -t <USER_NAME/IMAGE_NAME:IMAGE_VERSION> <DOCKERFILE_PATH>
# Remove all stopped and running container records.
# Stop running containers first; command substitution can target many containers.
docker rm $(docker ps -aq)
Run an Nginx container
Map local port 8000 to port 80 in the container:
docker run --name <NAME> -d -p 8000:80 nginx
Bind-mount local site files into the Nginx document root:
docker run \
--name <NAME> \
-d \
-p 8000:80 \
-v <PATH>:/usr/share/nginx/html \
nginx
Write a Dockerfile
Common Dockerfile instructions include:
FROM <IMAGE>
RUN <COMMAND>
WORKDIR <PATH>
COPY <LOCAL_PATH> <DOCKER_PATH>
CMD ["node", "hw.js"]
A small Node example:
FROM node
RUN apt update && apt install nodejs -y
WORKDIR /app
COPY . .
CMD ["node", "/app/hello-world.js"]
The official Node image already contains Node.js, so the extra installation step is normally unnecessary.
Historical Ubuntu installation commands
The original article used the following APT workflow:
# Remove older packages.
apt-get remove docker docker-engine docker.io containerd runc
# Install repository prerequisites.
sudo apt update
sudo apt install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
# Add the Docker repository key.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg |
sudo apt-key add -
# Add a mirror repository.
sudo add-apt-repository \
"deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# List available versions.
sudo apt update
apt list -a docker-ce
# Install current packages.
sudo apt install docker-ce docker-ce-cli containerd.io
# Or install a specific historical version.
sudo apt install \
docker-ce=18.06.0~ce~3-0~ubuntu \
docker-ce-cli=18.06.0~ce~3-0~ubuntu \
containerd.io
apt-keyis deprecated and package repositories change over time. Use Docker’s current official Ubuntu installation guide rather than copying this historical section into a new production server.