Docker FAQ

1, Docker new port mapping

1. Introduction

When docker run creates and runs containers, you can specify port mapping rules through - p. However, once the container is generated, no command can be modified directly. Generally, there are two ways to add or modify mapping ports. The environment here is Ubuntu 20 04

  • New container
  • Modify container configuration file

2. There are two ways to modify a container

2.1 new container

This method is the simplest. Delete the original container and create a new one. When creating a new one, determine the port mapping;

Advantages and disadvantages

  • The advantage is simple and fast, and it is widely used in the test environment
  • The disadvantage is that it needs to be reconfigured every time, and the operation will be troublesome

2.2 modify container configuration file

  • View container

The hashofthecontainer is the hash value of the docker image, and the CONTAINER ID is the value of the first few digits

#View containers that docker is running
docker ps
#View details of a container
docker inspect containername
  • Close docker service

If it is not closed, the modified configuration information may be changed back to the original configuration file after restart

systemctl stop docker.service

There may be an alarm here, except for docker Service unit file and a docker Socket unit file... Docker Socket this is used for socket activation. This warning means that if you try to connect to the docker socket and the docker service is not running, the system will start docker automatically. Generally ignored

Warning: Stopping docker.service, but it can still be activated by: docker.socket

  • Modify file

The configuration file path of the container is / var / lib / docker / containers / [hash_of_the_container] / hostconfig json, open the configuration file, find the value of PortBindings, and add a new port to this json collection. Of course, there are many other information in these two configuration files that can be modified. The principle is the same

"PortBindings": {
    "8081/tcp": [
      {
        "HostIp": "",
        "HostPort": "8081"
      }
    ],
    "8085/tcp": [
      {
        "HostIp": "",
        "HostPort": "8085"
      }
    ]
  },

If in config v2. The port is also recorded in the JSON file and needs to be modified at the same time. The modified values are config - > exposed ports and networksettings - > ports

"Config": {
    "Hostname": "acf6f980b9d3",
    "Domainname": "",
    "User": "",
    "AttachStdin": false,
    "AttachStdout": false,
    "AttachStderr": false,
    "ExposedPorts": {
      "8081/tcp": {
        
      },
      "8085/tcp": {
        
      },
      "8088/tcp": {
        
      }
    },
"NetworkSettings": {
    "Ports": {
      "8081/tcp": [
        {
          "HostIp": "0.0.0.0",
          "HostPort": "8081"
        },
        {
          "HostIp": "::",
          "HostPort": "8081"
        }
      ],
      "8085/tcp": [
        {
          "HostIp": "0.0.0.0",
          "HostPort": "8085"
        },
        {
          "HostIp": "::",
          "HostPort": "8085"
        }
      ]
    }
  }
  • Restart docker service and container
systemctl restart docker.service

Advantages and disadvantages

  • The advantages are no side effects and simple operation
  • The disadvantage is that the entire docker service needs to be restarted. If multiple container services are running on the same host, other container services will be affected

2, Linux Dockerfile cannot be started normally after it is built

1. Problem description

The following is a JDK8docker environment based on Centos8 built by myself

FROM centos:8
MAINTAINER shawn
#Adding java and tomcat to the container will decompress automatically
ADD jdk-8u161-linux-x64.tar.gz /usr/local/
#Set the WORKDIR path during work access and login foothold
WORKDIR /
#Install vim editor
RUN yum -y install vim
#Configure java and tomcat environment variables
ENV JAVA_HOME /usr/local/jdk1.8.0_161
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH $PATH:$JAVA_HOME/bin

RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
EXPOSE 8081 8088
CMD ehco "Build successful"
ENTRYPOINT /bin/bash

Add the required jdk-8u161-linux-x64 tar. GZ and Dockerfile files are uploaded to the Linux directory to build docker -t lamp:1.0, After the construction is completed, run the container docker run -d -p 8081:8081 -p 8088:8088 -v /home/shawn/ZHD/dockerdata:/data --name lamp lamp:1.0. Then it is found that the container cannot be started normally. centos starts a container and adds the - d parameter, but docker ps or docker ps -a view has exited

2. Reason introduction

  • The docker container must have a foreground process to run. If there is no foreground process to execute, the container will exit automatically if it thinks it is idle
  • If the commands run by the container are not those that have been suspended (running top, tail, loop, tomcat, etc.), they will exit automatically
  • This is the mechanism of docker

3. Solution method

#Add the - it command for docker run
docker run -itd -p 8081:8081 -p 8088:8088 -v /home/shawn/dockerdata:/data --name lamp lamp:1.0

3, Docker container setting time zone

1. Problem description

The official image in Docker Hub is generally set to UTC time by default. While we are located in the East eighth District, we often need to set the time zone when starting the container or building our own image

2. Configuration process

My docker environment here is Ubuntu. There are two ways to change it

#Enter the running container
docker exec -it lamp /bin/bash
#View current date
date
#Display results
Tue Jan 11 08:42:26 CST 2022

The first method is to restart the docker container after running on the host

  • Copy the local localtime file to the container
docker cp /etc/localtime {container id}:/etc/localtime
  • Copy the local / usr/share/zoneinfo/Asia/Shanghai file to the container
docker cp  /usr/share/zoneinfo/Asia/Shanghai  {container id}:/etc/localtime

The second type enters the container for operation without restarting the container. If the internal files of the container do not exist, the first type needs to be used

  • Link host localtime
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

The third is to run the command when building the dockerfile

reference resources

https://blog.csdn.net/weixin_43885975/article/details/117809901

https://blog.csdn.net/jameskaron/article/details/105540549

Keywords: Operation & Maintenance Docker Container

Added by eashton123 on Sun, 13 Feb 2022 02:00:38 +0200