In the first three articles of this series, we learned how to install Docker on the Ubuntu operating system, and through practical exercises, we learned the implementation principle of isolating Docker and host operating system file directories from each other, as well as the working principle of Docker Volume. By manually modifying the Nginx Docker image, the purpose of customizing its homepage display is achieved.
- Docker practical tutorial from entry to improvement (1)
- Docker practical tutorial from entry to improvement (2)
- Docker practical tutorial from entry to improvement (3)
The third article in the series contains steps to create an SSL-enabled Nginx Docker image based on an existing Nginx image using Dockerfile
.
This article continues our Docker learning journey.
Exercise 1: Create a new Docker image from scratch based on an empty image scratch
When we use Dockerfile to build docker images, one way is to use official pre-configured container images. The advantage is that we don't have to build from scratch, which saves a lot of work, but at the cost of downloading a large image package.
For example, the nginx-based images returned by docker images on my machine each exceed 100MB, while a simple Ubuntu container exceeds 200MB. If the relevant software is installed, the size will be larger.
If our requirement is to ensure that the image size is as small as possible on the premise of building a Docker image that meets our actual business needs, what should we do?
The idea is to use an empty mirror scratch.
Create a new folder and use wget to download the rootfs.tar.xz archive.
wget -O rootfs.tar.xz https://github.com/debuerreotype/docker-debian-artifacts/raw/b024a792c752a5c6ccc422152ab0fd7197ae8860/jessie/rootfs.tar.xz
What is this nearly 30MB compressed package?
After decompression, you can see the content, including most of the common commands of the operating system.
wget -O nginx.conf https://github.diablo.corp/raw/slvi/docker-k8s-training/master/docker/res/nginx.conf
Create a new dockerfile
file and paste the following content into it:
FROM scratch
# set the environment to honour SAP's proxy servers
ENV http_proxy http://sap.corp:8080
ENV https_proxy http://sap.corp:8080
ENV no_proxy .sap.corp
# give yourself some credit
LABEL maintainer="Jerry Wang"
# add and unpack an archive that contains a Debian root filesystem
ADD rootfs.tar.xz /
# use the apt-get package manager to install nginx and wget
RUN apt-get update && \
apt-get -y install nginx wget
# use wget to download a custom website into the image
RUN wget --no-check-certificate -O /usr/share/nginx/html/cheers.jpg https://github.diablo.corp/raw/slvi/docker-k8s-training/master/docker/res/cheers.jpg && \
wget --no-check-certificate -O /usr/share/nginx/html/index.html https://github.diablo.corp/raw/slvi/docker-k8s-training/master/docker/res/cheers.html
# copy the custom nginx configuration into the image
COPY nginx.conf /etc/nginx/nginx.conf
# link nginx log files to Docker log collection facility
RUN ln -sf /dev/stdout /var/log/nginx/access.log && \
ln -sf /dev/stderr /var/log/nginx/error.log
# expose port 80 - the standard port for webservers
EXPOSE 80
# and make sure that nginx runs when a container is created
CMD ["nginx", "-g", "daemon off;"]
Execute the command to build the image:
docker build -t nginx-from-scratch1.0 .
Produced log:
Finally see the message that the image was successfully built.
Start a new nginx container based on this image named nginx-from-scratch:
localhost:1083, see the home page, indicating that this newly built image is working fine.
Exercise 2: Delete all running and exited Docker instances
After the exercises of the three articles in this series, we must have created a lot of Docker instances locally, and it is time to clean them up.
docker ps -a
can display the status of all docker instances, including those that have exited:
Add the -q parameter to display only the container id
Use this command to pass the input generated by docker ps -aq
as input to the docker rm
command to delete all container instances.
docker rm $(docker ps -aq)
Exercise 3: Running the specified web application in a Docker container
There are many ways to achieve this requirement described in the title. As an introduction, let's start with the easiest way.
First use the command docker ps
to make sure there is no currently running Docker instance.
Run the command docker run -it nginx
:
Then we open another terminal and use the docker ps
command to view the running container instance. The Up 54 seconds of Status means that 54 seconds have passed since the instance was started.
Enter this container instance with the command:
docker exec -it bbc5d48a761c /bin/sh
After entering, see the shell prompt #
, we can execute some common commands in the nginx container.
/usr/share/nginx/html is the location where the web application is stored in the nginx server. Now we just need to find a way to put our web application in this folder.
The question is how to get this web application into the corresponding folder inside the container.
I am using docker volume
to realize data sharing between docker and host. Use docker stop
to stop the original docker instance and start a new one.
Suppose I download the webapp folder to the ~
directory, and then use the following command line to mount the webapp folder to the same name folder in the nginx container:
docker run -d -p 1081:80 -v pwd
/webapp:/usr/share/nginx/html/webapp --name jerry-custom nginx
Finally, use the url localhost:8081/webapp to access the web application running in the Docker container.
Summarize
This article first introduces how to create a new Docker image based on the scratch empty image, so as to reduce the size of the final image file. It then describes how to remove all running and exited Docker instances using the command line. The last is a practical example, how to run a developed web application in a Docker container.
Links to the first three articles in this series:
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。