Bobcares

For every $500 you spend, we will provide you with a $500 credit on your account*

BLACK FRIDAY SPECIAL

*The maximum is $4000 in credits, Offer valid till December 6th, 2024, New Customers Only, Credit will be applied after purchase and expires after six (6) months

For every $500 you spend, we will provide you with a $500 credit on your account*

BLACK FRIDAY SPECIAL

*The maximum is $4000 in credits, Offer valid till December 6th, 2024, New Customers Only, Credit will be applied after purchase and expires after six (6) months

Docker – change container configuration in 4 ways

by | Nov 9, 2018

 

You’re running a web server in your Docker container. The web server is configured to listen on default HTTP ports – 80 for http and 443 for https (http-ssl).

For security reasons, you want to change the port to which it is listening. How can you do that?

In our role as Technical Support Providers for web hosting companies and infrastructure providers, we provision and manage Docker systems for various business purposes.

Changes in Docker container configuration is a task we perform as a part of this service. Here, we’ll see the different ways to do that.

When do you need to change Docker container configuration?

In Docker infrastructure, you can have containers that run any application or service – from web server to WordPress instances.

Suppose you have a Docker container which runs web server on port 80 and 443.

3b127172bfe8      https-portal:1    "/init"   3 months ago     Up 3 months     0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   https-portal

 

As port 80 is more susceptible to attacks, you want to modify this port configuration to another port, say 8080.

Using ‘docker update’ command, we can modify or restrict container resources. But Docker doesn’t have any straight-forward way to modify ports or network settings.

 

How to change Docker container configuration

To modify the container configuration such as port mapping, we can do one of these 4 workarounds.

 

1. Create new image

The easiest way out is to terminate the existing container and spin up a new one with the new ports. This is done by copying the image of the existing container and then creating a new container from this image.

During container creation, the new port mapping or configuration setting can be specified as parameters.

docker commit https-portal https-portal:2
docker run -p 8080:80 -td https-portal:2

 

But this method is not feasible when there are too many containers and you can’t handle the overhead of creating and terminating containers.

 

2. Edit config file

Each Docker container has a set of config files associated with it. The parameters of that container, such as Port mapping, would be specified in that file.

The config file is a json file at the location ‘ /var/lib/docker/containers/container-id/config.v2.json ‘. Using ‘docker inspect’, we can see the current port mappings of a container.

"PortBindings": {
"443/tcp": [
{
"HostIp": "",
"HostPort": "443"
}
],
"80/tcp": [
{
"HostIp": "",
"HostPort": "80"
}
]
},

 

After stopping the container, the config.v2.json file can be edited to update the corresponding entry for “Ports” and “NetworkSettings”. The “PortBindings” entry in hostconfig.json file is also updated.

"PortBindings":{"443/tcp":[{"HostIp":"","HostPort":"443"}],"80/tcp":[{"HostIp":"","HostPort":"80"}]}

 

After making the changes in config files, Docker service is restarted and container is started. It will then show the new port mapping in the results.

 

3. Modify Dockerfile

In hosting environments or development scenarios which need too many containers to be spun up, the easiest way to manage is using Dockerfile.

The Dockerfile contains the basic configuration settings for each container. A sample snippet would be:

domain.com:
container_name: domain.com
image: wordpress
mem_limit: 500m
restart: always
environment:
WORDPRESS_DB_HOST: XX.XX.XX.XX
WORDPRESS_DB_NAME: domain-wp
WORDPRESS_DB_USER: username
WORDPRESS_DB_PASSWORD: 'password'
VIRTUAL_HOST: domain.com
volumes: - /home/username/files/domain.com/:/var/www/html:rw

 

For changes to be made, the Dockerfile is updated for those containers. After making changes, the corresponding containers are recreated and others are left intact.

docker-compose -f dockerfile.yml up -d --no-recreate

 

By creating images out of the modified containers and using them to create more containers, we can easily reproduce the same configuration settings in future containers.

When changes are made, Dockerfile is revised appropriately and new images are build from those. We set proper labels to identify the changed images.

In Docker, it is possible to define configuration settings, keys, and other data in environment variables. These parameters can be specified in the entry point script of that container.

            "WorkingDir": "/var/www/html", 
           "Entrypoint": [ 
               "/entrypoint.sh" 
           ],

 

Defining the parameters in entry script allows containers to start into a defined configuration. Any changes in the configuration can be reflected by modifying this entry script and restarting the containers.

When a Docker container is started, these environment variables are retrieved from the entry point script and relevant files are inserted into the container before it is launched.

 

4. Use Docker volumes

Docker containers do not store persistent data. To ensure data is preserved in Docker, we use Docker volumes to store them.

In production servers such as in hosting environment, we ideally store the configuration files in Docker Volumes. When containers are started, the path to the volume is specified.

Docker Volumes can also be configured in the entry point script or Dockerfile. The configuration files specified in this docker volume can be used to define the settings of the Docker containers created.

 

Conclusion

Docker containers are widely used in DevOps and niche web hosting. Today we’ve discussed the various ways how our Docker Support Engineers change Docker container configuration in the Docker infrastructure we manage.

 

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

SEE SERVER ADMIN PLANS

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

7 Comments

  1. Amishel

    >> After stopping the container, the config.v2.json file can be edited to update the corresponding entry for “Ports” and “NetworkSettings”. The “PortBindings” entry in hostconfig.json file is also updated.

    After stopping the container, config.v2.json file for any httpd container looked same.

    Only the hostconfig.json file is to be updated for the “PortBindings” entry. This will automatically update the “Ports” and “NetworkSettings” entires in config.v2.json file when the container is started.

    Reply
  2. Ahmet Keskin

    I tried to edit the config file. I’ve realized that I have to stop Docker service before editing the config file. Thanks lot.

    Reply
    • Sijin George

      Hello Ahmet,
      Yes, you have to stop Docker before editing the config file. Glad to know that article helped.

      Reply
  3. Adrian Garside

    Editing the json isn’t working for me. I stop the container, make the edit, confirm it’s present in the json, restart the container and then check the json again and my edit is gone.

    Reply
    • Hiba Razak

      Hi,
      Please contact our support team via live chat(click on the icon at right-bottom).

      Reply
  4. Atul

    Once the build is complete, an image will appear in the Images tab. Select the image name to see its details. Select Run to run it as a container. In the Optional settings remember to specify a port number (something like 8089).

    I m stuck at this on the container, can someone help, is it file that I need to change in editor or I can do the change in UI

    Reply
    • Hiba Razak

      Hello Atul,
      Our experts can help you with the issue.we will be happy to talk to you through our live chat(click on the icon at right-bottom).

      Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF