Docker
Contents
- 1 Installing Docker
- 2 Installing Docker Machine
- 3 Installing Docker SDK
- 4 Starting Docker Service
- 5 Listing Images
- 6 Pulling an Image
- 7 Running Container
- 8 File Transfer
- 9 Listing Containers
- 10 Displaying Container Info
- 11 Attaching to a Container
- 12 Networking
- 13 Clean-up
- 14 Building an Image
- 15 VPN Issue
- 16 References
Installing Docker
To install Docker from Docker repository:
$ dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo $ dnf install docker-ce docker-ce-cli containerd.io
See Install Docker Engine on Fedora.
To install Docker from Fedora:
$ dnf install docker
Installing Docker Machine
To install Docker Machine:
$ curl -L https://github.com/docker/machine/releases/download/v0.16.2/docker-machine-`uname -s`-`uname -m` >/tmp/docker-machine && chmod +x /tmp/docker-machine && sudo cp /tmp/docker-machine /usr/local/bin/docker-machine
See also:
Installing Docker SDK
To install Docker SDK for Python:
$ pip install docker
To test the installation:
$ python >>> import docker >>> client = docker.from_env() >>> print(client.containers.run("alpine", ["echo", "hello", "world"]))
See also:
Starting Docker Service
Start Docker service with the following command:
$ systemctl start docker $ systemctl enable docker
Verify with the following command:
$ docker info $ docker run hello-world $ docker run -it ubuntu bash
Listing Images
$ docker images
Pulling an Image
PHP
$ docker pull registry.access.redhat.com/rhscl/php-56-rhel7
MySQL
$ docker pull mysql
PKI
$ docker pull dogtagpki/pki-ci:f26
IPA
$ docker pull freeipa/freeipa-server
Running Container
To run a command in a new container:
$ docker run --name=<container> <image> /bin/echo "Hello World!"
To run an interactive shell in a new container:
$ docker run -ti <image> /bin/bash
To run a container in the background:
$ docker run --name=<container> --detach -i <image>
To run a command in an existing container:
$ docker exec <container> <command>
To run an interactive shell in an existing container:
$ docker exec -ti <container> /bin/bash
File Transfer
To copy a file to container:
$ docker cp <source> <container>:<target full path>
To copy a file from a container:
$ docker cp <container>:<source full path> <target>
Listing Containers
To list active containers:
$ docker ps
To list all containers:
$ docker ps --all
To list certain columns only:
$ docker ps --format 'table {{.Names}},{{.Image}},{{.Command}}'
Displaying Container Info
$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <name or ID>
Attaching to a Container
To attach to an existing container:
$ docker attach <container>
To detach, press Ctrl-PQ.
Networking
To list networks:
$ docker network ls
To create a network:
$ docker network create <network>
To attach containers:
$ docker network connect <network> <container>
Clean-up
To remove a container:
$ docker rm -f <container>
To remove all stopped containers:
$ docker container prune -f
To remove an image:
$ docker rmi -f <image>
To remove all unused images:
$ docker image prune --all -f
To clean up the system:
$ docker system prune -a
Building an Image
Creating a Dockerfile
FROM fedora:27 LABEL maintainer="Dogtag PKI Team <pki-devel@redhat.com>" ENV container=docker LANG=en_US.utf8 LANGUAGE=en_US.utf8 LC_ALL=en_US.utf8 RUN echo 'deltarpm = false' >> /etc/dnf/dnf.conf RUN dnf update -y dnf RUN dnf install -y dnf-plugins-core sudo wget RUN dnf install -y python-srpm-macros RUN dnf install -y @buildsys-build @development-tools # Enable PKI COPR. RUN dnf copr enable -y @pki/10.6 RUN dnf builddep -y pki-base # Enable IPA COPR. RUN dnf copr enable -y @freeipa/freeipa-4-6 RUN dnf install -y freeipa-server \ freeipa-server-dns \ freeipa-server-trust-ad \ python-ipatests \ --best --allowerasing STOPSIGNAL RTMIN+3 VOLUME ["/freeipa", "/run", "/tmp"] ENTRYPOINT ["/usr/sbin/init"]
Building an image locally
To build an image from a Dockerfile:
$ docker build -t <image> <folder that contains Dockerfile>
Pushing an image to DockerHub
$ docker login --username=<username> $ docker tag <image> <repo>/<username>/<image>:<tag> $ docker push <repo><username>/<image>:<tag>
Building an image on DockerHub
- Link DockerHub to GitHub: https://hub.docker.com/account/authorized-services/
- Create automated build: https://hub.docker.com/add/automated-build/USER/
- Select pki-ci repository on GitHub.
- Configure the builds: https://hub.docker.com/r/USER/pki-ci/~/settings/automated-builds/
- Type: Branch
- Name: master
- Dockerfile location: /docker/f27
- Docker Tag Name: latest
- Check build status: https://hub.docker.com/r/USER/pki-ci/builds/
VPN Issue
If /etc/resolv.conf is configured to use 127.0.0.1 (e.g. due to VPN), Docker build may not work.
nameserver 127.0.0.1
To fix the problem, find the actual DNS server:
$ nmcli dev show | grep IP4.DNS IP4.DNS[1]: <DNS server>
Then edit /etc/sysconfig/docker-network and specify the following:
DOCKER_NETWORK_OPTIONS="--dns <DNS server>"
Then restart Docker:
$ systemctl restart docker
See also Containers cannot resolve DNS if docker host uses 127.0.0.1 as resolver.