Docker/Basics
(→list) |
|||
Line 31: | Line 31: | ||
download docker images for local deployment. | download docker images for local deployment. | ||
<nowiki>~$ sudo docker pull repository/software_name:tag</nowiki> | <nowiki>~$ sudo docker pull repository/software_name:tag</nowiki> | ||
− | ===list=== | + | ===images (list)=== |
get a listing of local available docker images | get a listing of local available docker images | ||
<nowiki>~$ sudo docker images</nowiki> | <nowiki>~$ sudo docker images</nowiki> | ||
+ | |||
===delete image=== | ===delete image=== | ||
delete a local docker image | delete a local docker image |
Revision as of 17:36, 18 December 2018
Overview | Continuous Integration (CI) | Source Control Management (SCM) | Containerization | Configuration
Contents |
Docker Basics
Installation
The Docker Way
Installation done on CentOS7. Install, start, enable for system startup.
~$ sudo yum check-update ~$ curl -fsSL https://get.docker.com/ | sh ~$ sudo systemctl start docker ~$ sudo systemctl enable docker
Using a repo
NOTE: Untested.
~$ sudo vim /etc/yum.repos.d/docker.repo [dockerrepo] name=Docker Repository baseurl=https://yum.dockerproject.org/repo/main/centos/7/ enabled=1 gpgcheck=1 gpgkey=https://yum.dockerproject.org/
Find and install from Repo
~$ sudo yum search docker-ce ~$ sudo yum install -y docker-ce ~$ sudo systemctl start docker ~$ sudo systemctl enable docker
Images
search
public Docker Hub (repo) for images of software builds. Includes both official (owner) created and public (consumer) created so be careful with what you download for obvious security reasons.
~$ sudo docker search software_name
pull
download docker images for local deployment.
~$ sudo docker pull repository/software_name:tag
images (list)
get a listing of local available docker images
~$ sudo docker images
delete image
delete a local docker image
~$ sudo docker rmi image_id
history
see a history of the docker image. It is important to know that the history only stacks based on the image_id the container was started up from. If you create multiple changes and commit them separately, the latest image will have all the updates, but will not the multiple comments if they were included in the commit. If you want a consistent linear growth of commit history in the image, you must start the container with the latest image_id before making updates.
~$ sudo docker history image_id
commit
create a new image based on the specified container. It is important to note that if you want to maintain a consistent linear growth of commit history in the image, you must start the container with the latest image_id before making updates or commits.
Also, as best practice it is recommended that you specify the repository where the docker image will be pushed to (whether public or private). Locally however, the repository name structure can be anything, but you should follow best practice to keep organized.
~$ sudo docker commit -m "comment" -a "Author" container_id repository/image_name:tag ie. ~$ sudo docker commit -m "installed telnet" -a "root" ce056f9a7d2f root_repo/centos-updated:version3
inspect
inspect an image to see what it is comprised of.
~$ sudo docker inspect image_id
Containers
run
There are a few different options to consider when running a container.
- interactive
-it
- add the -it option if you want to be interactive with the container, ie. immediately log in as root and have a shell.
~$ sudo docker run -it image_id ~$ sudo docker run -it image_id:latest /bin/bash (example command)
- standard
- no switches. This will run the container and execute the default command. In basic instances it would be
"/bin/bash"
which is an unattached shell that would immediate exit causing the container to stop running.
~$ sudo docker run image_id
- name with meaningful name
--name=meaningful_name
- add a meaningful name instead of allowing docker to randomly create one.
~$ sudo docker run --name=meaningful_name image_id
- hostname
--hostname=value
- specify a specific hostname for the container rather than allowing a random one to be generated.
~$ sudo docker run --hostname=defined_hostname image_id
- automatically delete containers upon exit
--rm
- a great time saving option that prevents you from having lots of stopped containers lingering.
~$ sudo docker run --rm image_id
- disconnected
-d
- also called daemon mode
~$ sudo docker run -d --name=meaningful_name image_name:tag
- container port(s)
-p -P
- start a container with a port or ports that pass through from the container to the host.
~$ sudo docker run -d --name meaningful_name -P image_name:tag -------- This binds a random host port from 32768+ to the container listening port. ~$ sudo docker run -d -p 8080:80 --name=meaningful_name image_name:tag -------- This binds the host port 8080 to the container port 80.
- volume mount
-v
- mount a volume from the host into a specific location in the container. Allows a static location for files that live outside of the containers.
~$ sudo docker -d -p 8080:80 --name=meaningful_name -v /host/path:/container/path image_name:tag ie. ~$ sudo docker -d -p 8080:80 --name=Webserver4 -v /home/user/www:/usr/share/ngnix/html nginx:latest
list
There are a couple of different main options to consider when listing containers.
- standard
- no switches. This will list all the currently running containers.
~$ sudo docker container ls
- all containers
-a
- add the -a switch if you want to see all containers including those that are not running.
~$ sudo docker container ls -a
- only ids
-q
- list containers but just show their id
~$ sudo docker container ls -q
remove
remove a container
~$ sudo docker rm container_id
remove all stopped containers
~$ sudo docker rm `sudo docker container ls -a -q`
restart
- restart
- restart a container
~$ sudo docker restart container_id
execute
- exec
- Sometimes you will need to execute a command within a contain. You can do this with the
exec
command.
~$ sudo docker exec container_id command ~$ sudo docker exec -it container_id /bin/bash ------- execute a shell on a container and attach interactively.
Docker Hub
Docker Hub allows you to store your images either in private or public repositories. Personal accounts get (1) free private repo, Organizations do not get any free private repos. Both can purchase private repos at any time through a subscription model.
login
login to the docker hub.
~$ sudo docker login -u username
pull
download docker images for local deployment.
~$ sudo docker pull repository/software_name:tag
push
push a new or updated docker image.
~$ sudo docker push repository/software_name:tag
logout
~$ sudo docker logout
Docker Files
Docker files are used to create images and automatically configure the software.
Essentials
- FROM image_name:tag
- specifies the base image to start from.
FROM debian:stable
- MAINTAINER
- specifies the maintainer of the image, generally your dockerhub account and email.
MAINTAINER user_account <[email protected]>
- RUN
- explicitly runs commands after the container starts.
RUN apt-get update && apt-get upgrade -y
- ENV
- sets an environmental variable that can be used in the container
ENV MYVALUE my-value
- EXPOSE
- exposes explicit container ports to the host
EXPOSE 80
- CMD
- generally used to start a process or service.
-D
because we are not running in a daemon modeFOREGROUND
because we want the service to run in the foreground.
CMD ["/usr/sbin/apache2ctl","-D","FOREGROUND"]