Millet Porridge

English version of https://corvo.myseu.cn

0%

Docker Series 5: Access to an exited container

Problem

Consider this scenario: For some reasons, a normal container exited. You have tried to start it, but nothing happens. Since “docker exec” only works on running containers, you have to wait for it to resume.

1
2
3
4
5
6
7
'use strict';
var http = require('http');
var server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3000, '0.0.0.0');
console.log('server started');
1
2
3
FROM node:10.17.0-alpine
COPY main.js main.js
ENTRYPOINT ["node", "main.js"]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
docker build -t test .
docker run --name force-exec -ti -d test
docker exec -ti force-exec /bin/sh

/ $ vi main.js
# I delete the `var http = require('http');`

/ $ cat main.js
var server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3000, '0.0.0.0');

# now, we stop it
docker stop force-exec

# we want to restart the container, but it won't work anymore.
docker start force-exec

Solution

Here I offer a solution, which is to dump the stopped container and make it a image. After that, we could replace the CMD or ENTRYPOINT to run a new container.

Dump the container

1
2
3
4
5
6
7
8
9
10
# the exited container
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
64a9b5143f77 test "node main.js" 6 minutes ago Exited (1) 3 minutes ago force-exec

# use `docker commit` to convert it a image
$ docker commit force-exec force-exec:debug

# run a new container to debug
$ docker run -ti --entrypoint=/bin/sh force-exec:debug

You can see more about commit in the official docs.