Install Docker under ubuntu

  • 2020-05-14 05:46:41
  • OfStack

Docker is dotCloud just announced in recent months, an open source engine, is designed to provide one kind of application deployment automation solutions, that is simple, quickly create a container on Linux system (similar to a virtual machine) and deploy and run the application on the container, and through the configuration file can easily realize the automation of the application installation, deployment and upgrade, very convenient. Because of the use of containers, it is convenient to separate the production environment from the development environment without affecting each other. This is one of the most common ways of playing docker. More gameplay options include large-scale web applications, database deployment, continuous deployment, clustering, test environments, service-oriented cloud computing, virtual desktop VDI, and more.

Note: since Docker needs to be installed in Linux Kernel 3.8 and above, Ubuntu system is officially recommended. There are two options: Ubuntu 12.04 lts or the latest Ubuntu 13.10. This article prefers LTS.

1. Update the Ubuntu kernel

Update the kernel to 3.8.0-25 using the following command line


sudo apt-get install linux-image-3.8.0-25-generic
sudo apt-get install linux-headers-3.8.0-25-generic

When you're done, restart your computer and check to see if the kernel has been updated successfully with the command "uname-r."

2. Install lxc-docker


root@ubuntu: sudo apt-get install software-properties-common # increase  add-apt-repository  The command 
root@ubuntu: sudo apt-get install python-software-properties 
root@ubuntu: sudo add-apt-repository ppa:dotcloud/lxc-docker # increase 1 a ppa The source, such as: ppa:user/ppa-name
root@ubuntu: sudo apt-get update # Update the system 
root@ubuntu: sudo apt-get install lxc-docker

3. Test whether doctor is installed successfully


root@ubuntu:~# docker # The following information appears docker Successful installation 
Usage: docker [OPTIONS] COMMAND [arg...]
-H=[tcp://127.0.0.1:4243]: tcp://host:port to bind/connect to or unix://path/to/socket touse

A self-sufficient runtime for linux containers.
...

4, Hello World

4.1 download official ubuntu image


linjiqin@ubuntu:~$ sudo docker pull ubuntu #pull The command needs to go to a foreign mirror warehouse and pull the mirror because GFW The relationship, pull the possibility of failure is very large 

4.2. Run hello world


linjiqin@ubuntu:~$ sudo docker run ubuntu /bin/echo hello world

5. docker common commands

5.1, docker3 command operation modes

docker has three ways to run commands: transient, interactive, and daemon.

a, transitory mode: the "hello world". After the execution of the command, container terminates but does not disappear. sudo docker ps - can be used

a look at 1 for all container. The first one is container, which can be executed once more:


linjiqin@ubuntu:~$ sudo docker start container_id

But this time you can't see hello world, you can only see ID, you can only see ID with logs:


linjiqin@ubuntu:~$ sudo docker logs container_id

You can see two "hello world" because this container has been run twice.

b, interactive mode


linjiqin@ubuntu:~$ sudo docker run -i -t image_name /bin/bash #image_name for docker Name of the mirror 

c, daemon

Let the software run as a long time service, that's SAAS!
For example, a script that prints in an infinite loop (replace it with memcached, apache, etc., still the same!) :


linjiqin@ubuntu:~$ CONTAINER_ID=$(sudo docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done")

View its output outside container


linjiqin@ubuntu:~$ sudo docker logs $CONTAINER_ID

Or connect to the container for real-time viewing


root@ubuntu: sudo apt-get install software-properties-common # increase  add-apt-repository  The command 
root@ubuntu: sudo apt-get install python-software-properties 
root@ubuntu: sudo add-apt-repository ppa:dotcloud/lxc-docker # increase 1 a ppa The source, such as: ppa:user/ppa-name
root@ubuntu: sudo apt-get update # Update the system 
root@ubuntu: sudo apt-get install lxc-docker
0

Termination of the container


root@ubuntu: sudo apt-get install software-properties-common # increase  add-apt-repository  The command 
root@ubuntu: sudo apt-get install python-software-properties 
root@ubuntu: sudo add-apt-repository ppa:dotcloud/lxc-docker # increase 1 a ppa The source, such as: ppa:user/ppa-name
root@ubuntu: sudo apt-get update # Update the system 
root@ubuntu: sudo apt-get install lxc-docker
1

5.2. docker ps


root@ubuntu: sudo apt-get install software-properties-common # increase  add-apt-repository  The command 
root@ubuntu: sudo apt-get install python-software-properties 
root@ubuntu: sudo add-apt-repository ppa:dotcloud/lxc-docker # increase 1 a ppa The source, such as: ppa:user/ppa-name
root@ubuntu: sudo apt-get update # Update the system 
root@ubuntu: sudo apt-get install lxc-docker
2

Note:

Please refer to sudo docker ps-h for other USES

b. Another way to get your program to run in daemon mode is to set USER to daemon in Dockerfile

5.3. docker export


linjiqin@ubuntu:~$ container_id=`docker run -d <image_name> ls`
linjiqin@ubuntu:~$ docker export $container_id > image.tgz

5.4. docker import


root@ubuntu: sudo apt-get install software-properties-common # increase  add-apt-repository  The command 
root@ubuntu: sudo apt-get install python-software-properties 
root@ubuntu: sudo add-apt-repository ppa:dotcloud/lxc-docker # increase 1 a ppa The source, such as: ppa:user/ppa-name
root@ubuntu: sudo apt-get update # Update the system 
root@ubuntu: sudo apt-get install lxc-docker
4

5.5. docker port


root@ubuntu: sudo apt-get install software-properties-common # increase  add-apt-repository  The command 
root@ubuntu: sudo apt-get install python-software-properties 
root@ubuntu: sudo add-apt-repository ppa:dotcloud/lxc-docker # increase 1 a ppa The source, such as: ppa:user/ppa-name
root@ubuntu: sudo apt-get update # Update the system 
root@ubuntu: sudo apt-get install lxc-docker
5

5.6 delete the container

5.6.1 delete all containers


root@ubuntu: sudo apt-get install software-properties-common # increase  add-apt-repository  The command 
root@ubuntu: sudo apt-get install python-software-properties 
root@ubuntu: sudo add-apt-repository ppa:dotcloud/lxc-docker # increase 1 a ppa The source, such as: ppa:user/ppa-name
root@ubuntu: sudo apt-get update # Update the system 
root@ubuntu: sudo apt-get install lxc-docker
6

5.6.1 delete a specific container


root@ubuntu: sudo apt-get install software-properties-common # increase  add-apt-repository  The command 
root@ubuntu: sudo apt-get install python-software-properties 
root@ubuntu: sudo add-apt-repository ppa:dotcloud/lxc-docker # increase 1 a ppa The source, such as: ppa:user/ppa-name
root@ubuntu: sudo apt-get update # Update the system 
root@ubuntu: sudo apt-get install lxc-docker
7

5.7. docker quick reference


linjiqin@ubuntu:~$ sudo docker images # View local image 
linjiqin@ubuntu:~$ sudo docker attach $CONTAINER_ID # Start the 1 Two existing ones docker The instance 
linjiqin@ubuntu:~$ sudo docker stop $CONTAINER_ID # stop docker The instance 
linjiqin@ubuntu:~$ sudo docker logs $CONTAINER_ID # To view docker The instance runs the log to ensure normal operation 

linjiqin@ubuntu:~$ sudo docker inspect $CONTAINER_ID # To view container Instance properties, such as ip , etc.  

sudo docker run -t -i -v /home/linjiqin/dev/docker:/home/mycontainer:rw -p 8000:8000 mydocker /bin/bash

The command written earlier to run our mirror USES the above for reference, which will mount the local folder and map container's 8000 port to the host's 8000 port

/ home/linjiqin/dev/docker to mount local folders, need to be created in advance

/home/mycontainer maps to the docker path, which is created by executing the above command


Related articles: