Back to all posts
Common Docker Commands
DevOps
-
Docker official website
-
Official site: https://www.docker.com/
-
Docker concepts
-
image: a packaged snapshot
-
container: a running instance of an image
-
repository: a place to store images
Docker Installation
## mac
brew install --cask --appdir=/Applications docker
## centos
sudo yum update
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
sudo systemctl enable docker
sudo docker --version
Common Docker Images
# alpine
docker run -itd alpine /bin/sh
# memcached
docker run -d -p 11211:11211 memcached
Frequently Used Commands
# pull
docker pull image-name
# images
docker images
docker rmi image-id
docker build -t image-name:tag .
# container
docker run -d -p host-port:container-port image-name
docker ps -a
docker rm -f container-id
docker logs container-id
docker exec -it container-id /bin/bash
# container run cpu memory
docker run -d -p host-port:container-port --cpus="2" --memory="200m" --restart="always" --name="container-name" image-name
docker inspect --format='{{.HostConfig.NanoCpus}}' <container_id_or_name>
docker inspect --format='{{.HostConfig.Memory}}' <container_id_or_name>
docker run
## Basic
docker run ubuntu:15.10 /bin/echo "Hello world"
## Interactive
docker run -i -t ubuntu:15.10 /bin/bash
## Background
docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
## name
docker run -itd --name ubuntu-test ubuntu /bin/bash
Docker Containers
## List running containers
docker ps
## List all containers, including stopped ones
docker ps -a
## Stop a running container
docker stop id
## Restart a container
docker restart id
## Remove a container
docker rm -f 1e560fca3906
## View container logs
docker logs id
## Enter a container
docker exec -it 243c32535da7 /bin/bash
## Export a container
docker export 1e560fca3906 > ubuntu.tar
## Import a container
docker import - test/ubuntu:v1
## View container processes
docker top id
Docker Images
## List images
docker images
## Pull an image
docker pull ubuntu
## Search for an image
docker search httpd
## Remove an image
docker rmi hello-world
## Update an image
docker commit -m="has update" -a="runoob" e218edb10161 runoob/ubuntu:v2
## Build an image
### dockerfile
#### Each instruction creates a new layer on the image. Each instruction prefix must be uppercase.
#### The first FROM specifies which base image to use.
#### RUN tells Docker to execute commands inside the image, installing packages, etc.
#### Then, use the Dockerfile to build an image via the docker build command.
FROM centos:6.7
MAINTAINER Fisher "fisher@sudops.com"
RUN /bin/echo 'root:123456' |chpasswd
RUN useradd runoob
RUN /bin/echo 'runoob:123456' |chpasswd
RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
EXPOSE 22
EXPOSE 80
CMD /usr/sbin/sshd -D
### build
docker build -t runoob/centos:6.7 .
## Tag an image
docker tag 860c279d2fec runoob/centos:dev