Self-Paced Training (1) - Introduction to Docker

时间:2023-02-10 10:58:43

helloworld:

wget -qo- https://get.docker.com/ | sh

sudo docker run hello-world

sudo usermod -aG docker johnnytu

docker run hello-world

Install Docker

  1. Follow the instructions at https://docs.docker.com/installation/ to install the latest Docker maintained Docker package on your preferred operating system
  2. Run the hello-world container to test your installation: sudo docker run hello-world
  3. Add your user account to the docker group: sudo user mod -aG docker <user>
  4. Logout of your terminal and log back in for the changes to take effect
  5. Verify that you can run the hello-world container without using sudo: docker run hello-world

sudo docker version

Docker Machine

Tools that provisions Docker hosts and installs the Docker Engine on them

Docker Swarm

Tools that clusters many Engines and schedules containers

Docker Compose

Tools to create and manage multi-container applications

Create a Docker Hub Account

  1. Go to https://hub.docker.com/account/signup/ and signup for an account if you do not already have one
  2. Find your confirmation email and activate your account
  3. Browse some of the repositories
  4. Search for some images of your favorite dev tools, languages, servers etc… examples: Java, Perl, Maven, Tomcat, NGINX, Apache

Display local image

sudo docker images

Creating a Container

sudo docker run [options] [image] [command] [args]

image is specified with repository:tag

Examples

docker run ubuntu:14.04 echo “hello world”

docker run ubuntu ps ax

docker run -i -t ubuntu:14.04 /bin/bash

The -i flag tells docker to connect to STDIN on the container

The -t flag specifies to get a pseudo-terminal

Run a Container and get Terminal Access

  1. Create a container using the ubuntu 14.04 image and connect to STDIN and a terminal: sudo docker run -i -t ubuntu:14.04 /bin/bash
  2. In your container, create a new user using your first and last name as the username: adduser <username>
  3. Add the user to the sudo group: adduser <username> sudo
  4. Exit the container: exit
  5. Notice how the container shut down
  6. Once again run: sudo docker run -i -t ubuntu:14.04 /bin/bash
  7. Try and find your user
  8. Notice that it does not exist

Ctrl + P + Q

Container ID

Containers can be specified using their ID or name

Long ID and short ID

Short ID and name can be obtained using docker ps command to list containers

Long ID obtained by inspecting a container

Running in Detached Mode

Also known as running in the background or as a daemon

Use -d flag

To observe output use docker logs <container id>

docker run -d centos: ping 127.0.0.1 -c 

List Your Containers

  1. docker run -d centos:7 ping 127.0.0.1 -c 50
  2. List your containers by running: docker ps
  3. Notice the cents container running
  4. run: docker ps -a
  5. Notice all the containers created from the previous exercises

A More Practical Container

Run a web application inside a container

The -P flag to map container ports to host ports

Create a container using the tomcat image, run in detached mode and map the tomcat ports to the host port: docker run -d -P tomcat:7

Run a Web Application Container

  1. docker run -d -P tomcat:7
  2. Check your image details by running docker ps
  3. Notice the port mapping. The container’s port 8080 is mapped to a random port on your host machine: 0.0.0.0:49155->8080/tcp
  4. Go to <your linux server url>:<port number> and verify that you can see the Tomcat page