Docker Community Forums

Share and learn in the Docker community.

Commit vs build

Hello,
I have a question about comparison build and commit
Example : image mongo.

  1. run -it --name M1 mongo /bin/bash
    then inside contener:

apt-get update && apt-get install ssh
useradd test
echo “test:PASS” | chpasswd

on the host machine i do: docker commit M1 mongo-m1

2 The second way i make Dockerfile:
FROM mongo:latest
RUN apt-get -y update && apt-get -y install ssh
RUN useradd test
RUN echo “test:PASS” | chpasswd

and then:
docker build -t mongo-m2 .

And finally mongo-m1 does not work properly. ie:

docker run -d mongo-m1 does not create running contener
and
docker run -d mongo-m2 works fine

Any suggestions ?

Regards , Olaf

Never use docker commit. docker build is more reproducible, and there are a couple of actions (like setting CMD and ENTRYPOINT) that are important and difficult to do via docker commit.

(If you start with docker commit, you’ll eventually get to a point where you need to update the image, and wish you had written down the commands you had run to build the image originally…which is just a Dockerfile.)

Your two paths look like they should probably be close to equivalent, and I’m not sure why (or how) your committed image doesn’t work, but since your docker build path does work, I’d just stick with that and not worry too much about it.

Thanks for answer,
There is strange especially because committed system works fine when i started it in interactive mode (–it /bin/bash)
And i have to start mongod manually …