Everyday docker commands


I am starting to work more on docker. Here is my online notes, and I will keep adding new stuffs.

# Pull image from a repository: dokcer pull <repo/image>
Example:
docker pull registry.access.redhat.com/rhel

# Push image to a repository: dokcer pull <repo/image>
Example:
docker push docker-reg.mylab.local/soe

# Create a new container: docker run [options] [command]
# -v: mount host bin/directory to container
# –rm: remove container after exit
# –name: name the container
# -d: daemon mode
# -p: port mapping host_port:container_port
Examples:
docker run -v /bin:/bin –rm rhel less /etc/redhat-release
docker run -v /usr/sbin:/usr/sbin –rm rhel /usr/sbin/ip addr show eth0
docker run -v /usr/sbin:/usr/sbin –name=myip rhel /usr/sbin/ip addr show eth0
docker run -d -p 8000:8000 –name=”python_web” -v /usr/sbin:/usr/sbin -v /usr/bin:/usr/bin -v /usr/lib64:/usr/lib64 -w /var/www/html -v /var/www/html:/var/www/html rhel /bin/python -m SimpleHTTPServer 8000
docker run –name=”log_test” -v /dev/log:/dev/log –rm rhel logger “testing logging”
docker run -p 9999:80 –rm -i rhel:rhel7_httpd /usr/sbin/httpd -DFOREGROUND

# Run an existing container: docker start
# -i: interactive mode
# -a: attach container’s stdout and stderr to the process
Example:
docker start python_web
docker start -i myip
docker start -a -i python_web

# Inspect the metadata of a container: docker inspect
# –format: inspect a specific metadata
Examples:
docker inspect python_web
docker inspect –format='{{.NetworkSettings.IPAddress}}’ python_web

# Run command in container: docker exec -it
# -i: interactive mode
# -t: allocate a psesudo-tty, by default is false
Example:
docker exec -it python_web /bin/bash

# Stop a container: docker stop
Example:
dokcer stop python_wb

# Send a signal to container: docker kill [–signal=”xxx”]
Example:
docker kill [–signal=”SIGHUP”] python_web

# Remove containers: docker rm
# To remove all containers: docker rm $(docker ps -aq)
Example:
docker rm python_web myip

# Commit new image: docker commmit
# -m: comments
# -a: author
Example:
docker commit -m “RHEL7 with httpd” -a “Jackie Chen” focused_leakey rhel:rhel7_httpd

# Tag a image: docker tag repo:tags
Example:
docker tag 3310574481b2 myrepo:rhel7_httpd

# Save image to a tarball file: docker save
# -o: output file
Example:
docker save -o rhel7_httpd.tar rhel:rhel7_httpd

# Import docker image from a tarball file: docker import –
Example:
cat rhel7_httpd.tar | docker import – myrepo

# Remove images: docker rmi
# Remove all images: docker rmi $(docker images -a -q)
Example:
docker rmi myrepo:latest

# Build docker image
# Dockerfile syntax checker https://access.redhat.com/labs/linterfordockerfile/#/
Example:
[root@1004521 httpd-project]# vi Dockerfile
# RHEL7 httpd docker image
# Version 1
FROM registry.access.redhat.com/rhel
MAINTAINER Jackie Chen
# Update image
RUN yum update -y
RUN yum install httpd -y
# Create an test index file
RUN bash -c ‘echo “web server is running” >> /var/www/html/index.html’
[root@1004521 httpd-project]# docker build -t rhel .

# Check changes on a container’s filesystem: docker diff
Example:
docker diff python_web

# Check container resources usage: docker stats …
Example:docker stats python_web

# Check docker system wide info: docker info

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s