Docker – Part 3 – Installation

Add docker repo

sudo tee /etc/yum.repos.d/docker.repo <<-‘EOF’
[dockerrepo] name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7/
enabled=1 gpgcheck=1 gpgkey=https://yum.dockerproject.org/gpg
EOF

Install docker package

sudo yum install docker-engine
systemctl start docker
systemctl enable docker

Basic commands

# Search for images available on Docker Hub
[root@dockervn ~]# docker search centos

# Download a centos image from Docker Hub
[root@dockervn ~]# docker pull centos

# Show images in local host
[root@dockervn ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 05188b417f30 5 days ago 196.8 MB

# Start a container
[root@dockervn ~]# docker run -i -t centos /bin/bash
[root@0dd83d74d524 /]# uname -a
Linux 0dd83d74d524 3.10.0-229.el7.x86_64 #1 SMP Fri Mar 6 11:36:42 UTC 2015 x86_64 x86_64 x86_64

# Start a container and set the name for container
[root@dockervn ~]# docker run -i -t –name=webServer centos /bin/bash

# To return the host and keep the container running
press CTRL + P + Q

# To view all containers
[root@dockervn ~]# docker ps –a

# To remove a container
[root@dockervn ~]# docker rm container_ID

# To remove a image
[root@dockervn ~]# docker rmi image_ID

# To view the list of containers is running
[root@dockervn ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS NAME
0dd83d74d524 centos “/bin/bash” 5 minutes ago Up 5 minutes trusting_nobel

# To connect to container again
[root@dockervn ~]# docker attach 0dd83d74d524
OR
[root@dockervn ~]# docker attach trusting_nobel
[root@0dd83d74d524 /]#

# To stop a container
[root@dockervn ~]# docker stop 0dd83d74d524

# Locate a container in local disk
[root@dockervn ~]# dir /var/lib/docker/containers/

Commit the Changes in a Container to a Docker Image

Install webs server in the running container.

[root@dockervn ~]# docker attach 885a5971fb77
[root@885a5971fb77 /]# yum install httpd

  • The changes will be stored in Docker filesystems. But these files are temporary by default.
  • If you stop the container and start it up again, all your changes will be lost: any files you previously deleted will now be back, and any new files or edits you made won’t be present.

To save your changes and create a new image

[root@dockervn ~]# docker commit -m “centos 7 httpd” -a “An Duong” 885a5971fb77 my/httpd

 When you commit an image, the new image is saved locally, that is, on your computer

[root@dockervn ~]# docker images
REPOSITORY     TAG IMAGE ID     CREATED     SIZE
my/httpd        latest ab3ca9736f0c     20 seconds ago     316.7 MB
centos         latest 05188b417f30 5 days ago         196.8 MB

Start a new container from created image and map the port of container with -p

# Start a new container and map port 80 from host to container
[root@dockervn ~]# docker run -i -t -p 80:80 my/httpd /bin/bash

# Start http service inside container
[root@8975da78c44e /]# /usr/sbin/httpd

# Check port mapping
[root@dockervn ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED PORTS
8975da78c44e my/httpd “/bin/bash” 11 minutes ago 0.0.0.0:80->80/tcp

# Access web server from another host
http://IP_ADDRESS_OF_HOST (not use ip of container)

Part 1: https://duongtuanan.wordpress.com/2016/07/12/docker-part-1-overview/
Part 2: https://duongtuanan.wordpress.com/2016/07/12/docker-part-2-overview/
Part 3: https://duongtuanan.wordpress.com/2016/07/12/docker-part-3-installation/
Part 4: https://duongtuanan.wordpress.com/2016/07/12/docker-part-4-manage-data-in-docker-containers/

About Terri

System Administrator @Netpower Datacenter

Posted on 12.07.2016, in Linux, Technical Articles and tagged , , . Bookmark the permalink. 3 Comments.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: