User Tools

Site Tools


docker:change_commit_image

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

docker:change_commit_image [2020/03/03 17:16]
admin created
docker:change_commit_image [2020/03/03 17:21] (current)
admin
Line 5: Line 5:
 When it comes to modifying a Docker image, our first thought is modifying the underlying Dockerfile. In today’s article, I’m going to show you another way to create and change a Docker image. We will do this using the docker commit command. When it comes to modifying a Docker image, our first thought is modifying the underlying Dockerfile. In today’s article, I’m going to show you another way to create and change a Docker image. We will do this using the docker commit command.
  
-“Modify a Docker image without modifying the underlying Dockerfile.” via @madflojo +===== Starting with a Base Image =====
- +
-Click To Tweet +
-Starting with a Base Image+
  
 In today’s article, we are going to take a base Redis container image and add a new user to the container. While this example may be a bit generic, it shows how we can use the docker commit command. In today’s article, we are going to take a base Redis container image and add a new user to the container. While this example may be a bit generic, it shows how we can use the docker commit command.
Line 14: Line 11:
 Docker’s commit command allows users to take a running container and save its current state as an image. This means to add our new user, we will need a running container. To get started, let’s go ahead and launch a Redis container with the docker run command. Docker’s commit command allows users to take a running container and save its current state as an image. This means to add our new user, we will need a running container. To get started, let’s go ahead and launch a Redis container with the docker run command.
  
 +<code bash>
 $ docker run -d redis  ​ $ docker run -d redis  ​
 99eebc7db55d7039dcf6f36726c2492003ab06249b009e7b080648f4cc168015 99eebc7db55d7039dcf6f36726c2492003ab06249b009e7b080648f4cc168015
 +</​code>​
  
 In the above command, we can see that we started a container in the background using the redis image. Let’s first verify that the container is in fact running with the docker ps command. In the above command, we can see that we started a container in the background using the redis image. Let’s first verify that the container is in fact running with the docker ps command.
  
 +<code bash>
 $ docker ps  ​ $ docker ps  ​
 CONTAINER ID        IMAGE               ​COMMAND ​                 CREATED ​            ​STATUS ​             PORTS               ​NAMES  ​ CONTAINER ID        IMAGE               ​COMMAND ​                 CREATED ​            ​STATUS ​             PORTS               ​NAMES  ​
 99eebc7db55d ​       redis               "​docker-entrypoint..." ​  49 seconds ago      Up 47 seconds ​      ​6379/​tcp ​           upbeat_panini 99eebc7db55d ​       redis               "​docker-entrypoint..." ​  49 seconds ago      Up 47 seconds ​      ​6379/​tcp ​           upbeat_panini
 +</​code>​
  
 Now that we have a running container, let’s verify that our new user doesn’t already exist. To do this, we will be using another Docker command, docker exec. Now that we have a running container, let’s verify that our new user doesn’t already exist. To do this, we will be using another Docker command, docker exec.
  
 +<code bash>
 $ docker exec -it upbeat_panini /​bin/​bash  ​ $ docker exec -it upbeat_panini /​bin/​bash  ​
 root@99eebc7db55d:/​data#​ root@99eebc7db55d:/​data#​
 +</​code>​
  
 In the above output, we can see that the docker exec command worked, but what did it do? Let’s take a second to explore what exactly the above command does. In the above output, we can see that the docker exec command worked, but what did it do? Let’s take a second to explore what exactly the above command does.
Line 36: Line 39:
 Now that we are logged into the container, let’s go ahead and verify if our “new” user already exists. We can do this by searching for the user’s username in the /etc/passwd file. Now that we are logged into the container, let’s go ahead and verify if our “new” user already exists. We can do this by searching for the user’s username in the /etc/passwd file.
  
 +<code bash>
 root@99eebc7db55d:/​data#​ grep example /etc/passwd root@99eebc7db55d:/​data#​ grep example /etc/passwd
 +</​code>​
  
 With the above output, we can see the user does not exist within this container. With the above output, we can see the user does not exist within this container.
-Adding a User and Saving the Image+ 
 +===== Adding a User and Saving the Image ===== 
  
 Since we have verified that the example user does not exist on a base redis container, let’s go ahead and add that user to our running container. To do this, we will use the useradd command. Since we have verified that the example user does not exist on a base redis container, let’s go ahead and add that user to our running container. To do this, we will use the useradd command.
  
 +<code bash>
 root@99eebc7db55d:/​data#​ useradd -g redis example root@99eebc7db55d:/​data#​ useradd -g redis example
 +</​code>​
  
 If we once again execute the grep command against the /etc/passwd file, we should see this user now exists. If we once again execute the grep command against the /etc/passwd file, we should see this user now exists.
  
 +<code bash>
 root@99eebc7db55d:/​data#​ grep example /​etc/​passwd  ​ root@99eebc7db55d:/​data#​ grep example /​etc/​passwd  ​
 example:​x:​1000:​999::/​home/​example:/​bin/​sh example:​x:​1000:​999::/​home/​example:/​bin/​sh
 +</​code>​
  
 Now that our user is added, let’s go ahead and exit our container going back to the host system. From there, we can execute the docker commit command to save our image changes. Now that our user is added, let’s go ahead and exit our container going back to the host system. From there, we can execute the docker commit command to save our image changes.
Line 54: Line 65:
 When executing the docker commit command, we will need to provide two parameters: the name of the running container upbeat_panini and the name of our desired image output testredis:​example. When executing the docker commit command, we will need to provide two parameters: the name of the running container upbeat_panini and the name of our desired image output testredis:​example.
  
 +<code bash>
 $ docker commit upbeat_panini testredis:​example  ​ $ docker commit upbeat_panini testredis:​example  ​
 sha256:​6f68e12ee78732258a4fdfedca3ab164a5c9ea330ed28c9cb0d531477706373b sha256:​6f68e12ee78732258a4fdfedca3ab164a5c9ea330ed28c9cb0d531477706373b
 +</​code>​
  
 In the above output, we can see that the docker commit command returned a sha256 hash. Seeing this hash indicates that our docker commit command was successful. In the above output, we can see that the docker commit command returned a sha256 hash. Seeing this hash indicates that our docker commit command was successful.
Line 61: Line 74:
 Let’s double-check that our image was in fact created using the docker images command. Let’s double-check that our image was in fact created using the docker images command.
  
 +<code bash>
 $ docker images testredis:​example  ​ $ docker images testredis:​example  ​
 REPOSITORY ​         TAG                 IMAGE ID            CREATED ​            ​SIZE  ​ REPOSITORY ​         TAG                 IMAGE ID            CREATED ​            ​SIZE  ​
 testredis ​          ​example ​            ​6f68e12ee787 ​       10 hours ago        183MB testredis ​          ​example ​            ​6f68e12ee787 ​       10 hours ago        183MB
 +</​code>​
  
 With the above, we can see our testredis image is in fact created. With the above, we can see our testredis image is in fact created.
  
-!Sign up for a free Codeship Account +===== Seeing the Differences ​===== 
-Seeing the Differences+
  
 With our new container image saved, let’s go ahead and see how well our docker commit worked. We can do this by issuing the same grep command via a docker run execution. With our new container image saved, let’s go ahead and see how well our docker commit worked. We can do this by issuing the same grep command via a docker run execution.
Line 74: Line 89:
 First, let’s verify that our same base redis container doesn’t have the example user. First, let’s verify that our same base redis container doesn’t have the example user.
  
 +<code bash>
 $ docker run redis grep example /etc/passwd $ docker run redis grep example /etc/passwd
 +</​code>​
  
 From the above, we can see that the example user doesn’t exist within our base redis image. This might be a bit confusing at first, but once we think about how Docker works, it makes complete sense. From the above, we can see that the example user doesn’t exist within our base redis image. This might be a bit confusing at first, but once we think about how Docker works, it makes complete sense.
Line 82: Line 99:
 While our base redis image doesn’t have our example user, what about testredis:​example?​ While our base redis image doesn’t have our example user, what about testredis:​example?​
  
 +<code bash>
 $ docker run testredis:​example grep example /​etc/​passwd  ​ $ docker run testredis:​example grep example /​etc/​passwd  ​
 example:​x:​1000:​999::/​home/​example:/​bin/​sh example:​x:​1000:​999::/​home/​example:/​bin/​sh
 +</​code>​
  
 As expected, our new testredis:​example image does include an example user. From here, we can use this image to create new containers or push it to a registry such as DockerHub. As expected, our new testredis:​example image does include an example user. From here, we can use this image to create new containers or push it to a registry such as DockerHub.
docker/change_commit_image.1583252194.txt.gz · Last modified: 2020/03/03 17:16 by admin