Docker

Some common used docker commands.

Commands

Container Basis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Create and run a container
docker run [<option(s)>] <image name> <command to run in the container>
# Stop a container
docker stop <container ID>
# Start a container has exited
docker start <container ID>
# Remove a container
docker rm -f <container ID>
# Remove container(s) has exited
docker container prune

# Check current running containers
docker ps
# Show a container's output
docker logs <container ID>
# Show processes inside a container
docker top <container ID>
# Check ports mapping
docker ports <container ID>

# Get into a background running container
docker attach # exit the container cause the container stops
docker exec -it <shell name> # start a new terminal of the container, could use ps -aux to see previous processes

option:

  • -t: terminal a terminal inside the container
  • -i: allow interaction with container’s STDIN
  • -d: run container in background mode
  • -P: map the port of container to a random local machine port
  • -p: map the port of container to a a customized local machine port
  • –name: name the container
  • –rm: clean the container when the container exit
  • -v: mount a local work directory to container’s directory

Container Advance

1
2
3
4
5
6
7
# Connect container together
docker network create -d bridege <network name>
# Run a container and add it to the network
docker run -itd --network <network name> <image name> /bin/bash

# Show network
docker network ls

option:

  • -d: set the type (bridge, overlay) of the docker network

Images

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Show images in local machine
docker images
# Check online images
docker search <image name>
# Download an image
docker pull <image name: version tag>
# Remove an image
docker rmi <image name>

# Export container as file
docker export <container ID> > <file name>
# Imp;ort file as image
cat <file name> | docker import - <image name>

# Create docker image from existing image
docker run -it <docker name> /bin/bash
# update the environment, exit
docker commit -m="comment" -a="author" <precontainer ID> <container name>:<tag>
# Set image id to an image
docker tag <image id> <image name>

# Create docker image from dockerfile
cat Dockerfile
# write content to dockerfile
docker build -t <image name> <folder path to dockerfile>