1

I've been working with a simple MySQL table using Docker Compose that only included ID and NAME column. I attempted to update my myDb.sql file that initially creates the table like this:

CREATE TABLE `Person` (
  `id` int(11) NOT NULL,
  `firstName` varchar(50) NOT NULL, // updated this column
  `lastName` varchar(50) NOT NULL  // added this column
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

I updated the NAME column to firstName, and added a lastName column.

I then stopped the Docker containers by running DOCKER-COMPOSE STOP.

I then restarted the Docker containers by running DOCKER-COMPOSE UP. I even tried DOCKER-COMPOSE RESTART.

The error message I was able to print in the console was this:

SELECT '', id, `firstName` From Person<br>Unknown column 'firstName' in 'field list'

This leads me to believe that I did not restart Docker Compose correctly.

How do I restart Docker Compose so that it runs the CREATE TABLE command?

Edit

Here is my docker-compose.yml file:

version: "3.7"
services:
www:
    build: .
    ports: 
        - "8001:80"
    volumes:
        - ./www:/var/www/html/
    links:
        - db
    networks:
        - default
db:
    image: mysql:5.7.13
    ports: 
        - "3306:3306"
    environment:
        MYSQL_DATABASE: myDb
        MYSQL_USER: user
        MYSQL_PASSWORD: test
        MYSQL_ROOT_PASSWORD: test
    volumes:
        - ./dump:/docker-entrypoint-initdb.d
        - persistent:/var/lib/mysql
    networks:
        - default
phpmyadmin:
    image: phpmyadmin/phpmyadmin
    links: 
        - db:db
    ports:
        - 8000:80
    environment:
        MYSQL_USER: user
        MYSQL_PASSWORD: test
        MYSQL_ROOT_PASSWORD: test
volumes:
    persistent:

My Dockerfile looks like this:

FROM php:7.0.30-apache 
RUN docker-php-ext-install mysqli
halfer
  • 19,824
  • 17
  • 99
  • 186
John Beasley
  • 2,577
  • 9
  • 43
  • 89
  • Can you share dockerfile for database ? – Prasanth May 19 '19 at 15:00
  • @Prasanth - I've added my docker-compose.yml file and my Dockerfile. – John Beasley May 19 '19 at 21:14
  • Did you update your sql file after the database container started running ? – Prasanth May 20 '19 at 02:46
  • @Prasanth - Yes. I altered the sql file. The column were originally 'id' and 'name'. I tried to change the column 'name' to 'firstName', and then add a new column called 'lastName'. – John Beasley May 20 '19 at 03:25
  • Did the table get created when building the image or when running the image or container ? – Prasanth May 20 '19 at 03:52
  • Have you tried the --force-create switch when running docker compose? Refer to the docker-compose up documentation for an explanation of the command. https://docs.docker.com/compose/reference/up/ – Athens Holloway May 21 '19 at 13:48
  • You can also try try deleting all containers "docker rm $(docker ps -a -q)" followed by deleting all images "docker rmi $(docker images -q)" if you want to force the containers to be created again. – Athens Holloway May 21 '19 at 13:51

3 Answers3

10

The docker-entrypoint-initdb.d mechanism only runs the first time a database container starts up, with an empty database. You'll need to explicitly docker-compose rm your containers after you docker-compose stop them to cause the current database to be deleted, and then a new empty table will be created in a new empty database.

If you need to preserve the data in an existing database, you are looking for a mechanism called a migration. The various Docker database images don't directly have migration support; this is almost always something that is packaged with your application-level database library (Ruby on Rails and Python's SQLAlchemy both have migration facilities, for instance).

Once you have a migration system anyways, it's probably better to just use that to create the initial database tables. docker-entrypoint-initdb.d makes a little more sense for database-level setup like creating initial users or loading a seed database dump, but in practice you will always need a migration system for changes like what you're describing.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • I don't need to preserve the data. I'm just testing right now, practicing. I did run the docker-compose rm as you suggested. I restarted the docker container, but I am receiving the same error. Any thoughts? – John Beasley May 19 '19 at 21:18
  • @JohnBeasley In this case, you have to remove the previously created volume. Note that, your data will be lost. – Arashsyh Jun 02 '20 at 14:42
2

Do docker-compose down whenever you update your schema, it will remove containers and docker network and do docker-compose up to bring your environment with your new schema. Hope it helps. If not, try updating to latest mysql image, the image you are using is almost 3 years old.

Prasanth
  • 507
  • 2
  • 10
  • I don't think I tried docker-compose down. All I did was docker-compose stop. I will give it a try and let you know. – John Beasley May 20 '19 at 15:13
  • 1
    that will not work. You have to remove the volume with MySQL data. So either rm the container or do docker-compose down -v (it will delete the volumes) and then docker-compose up – user1762087 Jun 03 '20 at 07:40
0

So I was thinking that restarting docker-compose would automatically run the CREATE TABLE query in the myDb.sql file. As indicated above, I changed the name of 'name' column to 'firstName' and added a column called 'lastName'.

Again, I am not sure if there was a command that actually does this, but in the end, I was able to alter the table in phpmyadmin. Once I altered the table there, now I am getting data back to the page with no errors.

John Beasley
  • 2,577
  • 9
  • 43
  • 89