Dhruv Prakash
Dhruv Prakash

Reputation: 390

How to create an image from a container after executing a command to run a server in that container?

I have a running container named nodeserver1. I issued the following command to actually run the node server docker exec -d nodeserver1 nodejs ipshow.js Now how do I create an image from nodeserver1 such that the next time I build a container from this image, I do not need to issue the exec command. I have tried to commit it using docker commit <container id> <some-new-name> but when I run the new container, it doesn't start the node server.

Upvotes: 0

Views: 54

Answers (1)

Stefano
Stefano

Reputation: 5076

You probably would like to use the multistage build: https://docs.docker.com/engine/userguide/eng-image/multistage-build/

Here's an example shamelessy copied from the docker documentation:

FROM golang:1.7.3
WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html  
COPY app.go .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

FROM alpine:latest  
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=0 /go/src/github.com/alexellis/href-counter/app .
CMD ["./app"]  

Probably this is a better example using named builds:

FROM golang:1.7.3 as builder
WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html  
COPY app.go    .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

FROM alpine:latest  
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /go/src/github.com/alexellis/href-counter/app .
CMD ["./app"]  

Here, the key is COPY --from=builder /go/src/github.com/alexellis/href-counter/app .. Notice how it uses --from=builder to fetch the artifact from the other image.

Upvotes: 1

Related Questions